package com.yumeng.common.helper;

import com.yumeng.common.exception.NormalDBException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wxd
 * @date 2021/9/22 14:13
 */
@Slf4j
public class DbHelper {

    public static boolean retBool(Integer result, String message) {
        return retBool(result, message, true);
    }
    public static boolean retBool(Integer result, String message, boolean thr) {
        boolean b = null != result && result == 1;
        if (!b) logFalse(String.valueOf(result), "1", thr, message);
        return b;
    }

    public static boolean retBoolWeak(Integer result) {
        return retBoolWeak(result, null);
    }
    public static boolean retBoolWeak(Integer result, String message) {
        boolean b = null != result && result >= 1;
        if (!b) logFalse(String.valueOf(result), "大于1", false, message);
        return b;
    }


    public static boolean retBool(Integer result, Integer targetCount, String message) {
        boolean b = null != result && result.equals(targetCount);
        if (!b) logFalse(String.valueOf(result), String.valueOf(targetCount), true, message);
        return b;
    }

    public static void checkDataCount(int dataCount, int targetCount, boolean thr){
        if (dataCount != targetCount){
            String msg = String.format("结果集数量异常：%s-%s", dataCount, targetCount);
            log.warn(msg);
            if (thr){
                throw new NormalDBException(msg);
            }
        }
    }

    private static void logFalse(String result, String targetCount, boolean thr, String message){
        message = message != null ? message : "结果集数量异常";
        String msg = String.format("%s:%s-%s", message, result, targetCount);
        log.warn(msg);
        if (thr){
            throw new NormalDBException(msg);
        }
    }

}
